package org.gavin.core.conf;

import lombok.extern.slf4j.Slf4j;
import org.gavin.core.enums.CommonStatus;
import org.gavin.core.enums.SafetyType;
import org.gavin.core.exception.SafetyException;
import org.gavin.core.service.safety.SafetyAbstract;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * SafetyFactory
 *
 * @author grl
 * @date 2024/1/17
 */
@Slf4j
@Configuration
public class SafetyFactory {

    private final Map<SafetyType, SafetyAbstract> safetyBeanMap = new ConcurrentHashMap<>(8);

    public SafetyFactory(List<SafetyAbstract> safetyList) {
        safetyList.forEach(safety-> safetyBeanMap.put(safety.TYPE(), safety));
    }

    public SafetyAbstract getSafety(SafetyType type) {
        SafetyAbstract beam = safetyBeanMap.get(type);
        if (null == type || null == beam) {
            throw SafetyException.le(CommonStatus.BEAN_NOT_FIND, "SafetyFactory", type);
        }
        return beam;
    }
}
